using System.Drawing;
using System;
using BarcodePrint;
using System.Drawing.Printing;
class Renderer {
	public Renderer(string fileName) {
		this.FileName=fileName;
		this.lines=System.IO.File.ReadAllLines(FileName);
		//todo:validation here
	}
	string[] barcodeSeparators=new string[] { ",","*",";" };
	string[] comments=new string[] { "--","//","*" };
	Bar39 bar;
	Font fnt=new Font(Program.Font,8);
	readonly int distanceBtwBarcodes=20;
	readonly int distanceBtwBLines=3;
	string FileName;
	public string[] lines;
	public void RenderResStamp(Graphics g,float ctrX,float ctrY,int rotation) {
		float sRad=1.8f;
		float bRad=4f;
		Pen pen=new Pen(Color.Black,0.2f);
		g.FillPie(Brushes.Black,ctrX-bRad,ctrY-bRad,bRad*2,bRad*2,90+rotation,90);
		g.FillPie(Brushes.Black,ctrX-bRad,ctrY-bRad,bRad*2,bRad*2,270+rotation,90);
		g.FillEllipse(Brushes.White,ctrX-sRad,ctrY-sRad,sRad*2,sRad*2);
		g.FillPie(Brushes.Black,ctrX-sRad,ctrY-sRad,sRad*2,sRad*2,0+rotation,90);
		g.FillPie(Brushes.Black,ctrX-sRad,ctrY-sRad,sRad*2,sRad*2,180+rotation,90);
		g.DrawEllipse(pen,ctrX-bRad,ctrY-bRad,bRad*2,bRad*2);
	}
public	Size RenderTextLine(Graphics g,string str,int top) {
		if(str!="---")
			g.DrawString(str.Substring(2).Trim(),fnt,Brushes.Black,Program.Margin,top);
		else
			g.DrawLine(Pens.Black,Program.Margin,top,800,top);
		return g.MeasureString(str,fnt).ToSize();
	}
	public Size RenderBarLine(Graphics g,string str,int top) {
		int left=Program.Margin;
		foreach(string s in str.Split(barcodeSeparators,StringSplitOptions.RemoveEmptyEntries))
		{
			if(s.Trim()=="")
				left+=Program.Margin*s.Length;
			else
			{
				if(s.TrimStart()!=s)
					left+=Program.Margin*(s.Length-s.TrimStart().Length);
				{
					bar=new Bar39(s.Trim(),Program.Narrow,Program.Wide,Program.Height);
					bar.Print(g,left,top);
					left+=(int)bar.Width+distanceBtwBarcodes;
				}
			}
		}
		return new Size(left,bar.Height);
		;
	}
public	bool IsCommentLine(string line) {
		foreach(string com in comments)
			if(line.StartsWith(com))
				return true;
		return false;
	}
	public void Render(PrintPageEventArgs e) {
		//e.HasMorePages=true;
		int top=Program.Margin;
		Size sz;
		foreach(string str in lines)
		{

			if(e.HasMorePages==false)
			{
				//string str=strLine.Trim();
				if(str.Trim()=="")
				{
					top+=distanceBtwBarcodes;
					continue;
				}

				else if(IsCommentLine(str.Trim()))
					sz=RenderTextLine(e.Graphics,str.Trim(),top);
				else
					sz=RenderBarLine(e.Graphics,str,top);
				top+=sz.Height+distanceBtwBLines;


				if(top>1500)
				{
					e.HasMorePages=true;
					if(str.Trim()=="")
					{
						top+=distanceBtwBarcodes;
						continue;
					}

					else if(IsCommentLine(str.Trim()))
						sz=RenderTextLine(e.Graphics,str.Trim(),top);
					else
						sz=RenderBarLine(e.Graphics,str,top);
					top+=sz.Height+distanceBtwBLines;
				}
			}



		}


		//g.DrawString(FileName,new Font(Program.Font,4),Brushes.Black,Program.Margin+10,Program.Margin/2);
		RenderResStamp(e.Graphics,Program.Margin+3,Program.Margin/2+3,15);
	}
}